﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace WPNEXT.Views
{
    public partial class PageSpliterPage : PhoneApplicationPage
    {
        public PageSpliterPage()
        {
            InitializeComponent();
        }

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(PageSpliterPage), new PropertyMetadata(OnSelectedIndexChanged));

        private static void OnSelectedIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as PageSpliterPage).OnSelectedIndexChanged(e);
        }

        private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != e.NewValue)
            {
                int oldIndex = (int)e.OldValue;
                int newIndex = (int)e.NewValue;
                UIElementCollection elements = ES.Children;
                Ellipse oldEl = (Ellipse)elements.ElementAt(oldIndex);
                if (oldEl != null)
                {
                    oldEl.Fill = new SolidColorBrush(Colors.White);
                }
                Ellipse newEl = (Ellipse)elements.ElementAt(newIndex);
                if (newEl != null)
                {
                    newEl.Fill = TranslateColor("FFFBC038");
                }
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void start_Click(object sender, RoutedEventArgs e)
        {
            MySpliter.SelectedIndex = 1;
        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            MySpliter.SelectedIndex = 2;
        }

        private SolidColorBrush TranslateColor(string hexColorStr)
        {
            byte a = System.Convert.ToByte(hexColorStr.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(hexColorStr.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(hexColorStr.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(hexColorStr.Substring(6, 2), 16);
            Color s = Color.FromArgb(a, r, g, b);
            return new SolidColorBrush(s);
        }
    }
}